<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRoles"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="RolesList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button size="mini" type="success">分配权限</el-button>
                  <el-button size="mini" type="primary" @click.native="EditRoles(row)">编辑</el-button>
                  <el-button size="mini" type="danger" @click.native="del(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="q.page"
                :page-sizes="[2, 5, 10, 20]"
                :page-size="q.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="hsizeChange"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
        <!-- 新增弹框 -->
        <el-dialog
          :title="isEdit?'编辑':'添加'"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          :visible.sync="showDialog"
          @closed="hclose"
        >
          <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="roleForm.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="roleForm.description" />
            </el-form-item>
          </el-form>
          <!-- 底部 -->
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6">
              <el-button size="small" @click="hcancel">取消</el-button>
              <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
            </el-col>
          </el-row>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import { addRoles, delRoles, getRolesList, updataRoles } from '@/api/settings'
export default {
  data() {
    return {
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        description: [{ required: true, message: '请填写角色描述', trigger: 'blur' }]
      },
      RolesList: [],
      q: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      isEdit: false
    }
  },
  created() {
    // 获取角色列表
    this.getRoles()
  },
  methods: {
    // 关闭弹框触发事件
    hclose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验结果
      this.$refs.roleForm.clearValidate()
    },
    // 添加角色
    addRoles() {
      this.showDialog = true
      this.isEdit = false
    },
    // 点击取消
    hcancel() {
      this.showDialog = false
    },
    // 做添加
    async todoAdd() {
      try {
        const res = await addRoles(this.roleForm)
        // console.log(res)
        this.$message.success(res.message)
        this.showDialog = false
        // 修复添加时的bug
        // 如果最后一页满了,页码加1
        if (this.total % this.q.pagesize === 0) {
          this.total++
        }
        // 跳转到最后一页
        this.q.page = Math.ceil(this.total / this.q.pagesize)
        this.getRoles()
      } catch (e) {
        this.$message.success(e.message)
      }
    },
    // 做编辑
    async todoEdit() {
      const res = await updataRoles(this.roleForm)
      // console.log(res)
      this.$message.success(res.message)
      this.showDialog = false
      this.getRoles()
    },
    // 点击提交
    hSubmit() {
      // 兜底校验
      this.$refs.roleForm.validate(valid => {
        // console.log(valid)
        if (!valid) return
        this.isEdit ? this.todoEdit() : this.todoAdd()
      })
    },
    // 删除角色
    async del(id) {
      // console.log(id)
      try {
        const res = await delRoles(id)
        // console.log(res)
        const result = await this.$confirm('你确定要删除吗', '提示', { type: 'warning' }).catch(e => e)
        if (result !== 'confirm') return
        this.$message.success(res.message)
        // 如果当前是最后一页并且页码大于0,页码 -1
        if (this.RolesList.length === 1 && this.q.page > 0) {
          this.q.page--
        }
        // 重新渲染页面
        this.getRoles()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 编辑
    EditRoles(row) {
      // console.log(row)
      this.showDialog = true
      this.isEdit = true
      // 完成数据回填
      this.roleForm = { ...row }
    },
    // 获得所有的角色列表
    async getRoles() {
      const res = await getRolesList(this.q)
      console.log(res)
      this.RolesList = res.data.rows
      this.total = res.data.total
    },
    // 切换页数
    hsizeChange(pagesize) {
      this.q.pagesize = pagesize
      this.getRoles()
    },
    // 切换每页多少条
    hCurrentChange(page) {
      this.q.page = page
      // 重新渲染
      this.getRoles()
    }
  }
}
</script>
<style lang="sass" scoped>

</style>
